//
//  SLProductDetailViewController.m
//  StarLight
//
//  Created by Duke.wu on 16/5/17.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLProductDetailViewController.h"
#import "SLNearlyProductDetailViewModel.h"
#import "SLMapViewController.h"
#import "SLHisGoodsViewController.h"
#import "SLBackMsgViewController.h"
#import "STPopup.h"
#import "SLShipOwnerVipLevelView.h"


@interface SLProductDetailViewController ()

@property (nonatomic, strong)SLNearlyProductDetailViewModel *goodsVM;

@property (nonatomic, strong) NSString *gid;

@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic ,strong) UILabel *noLB;

@property (nonatomic ,strong) UILabel *productNameLB;

@property (nonatomic ,strong) UILabel *startLB;

@property (nonatomic ,strong) UILabel *finalLB;

@property (nonatomic ,strong) UILabel *packageTypeLB;

@property (nonatomic ,strong) UILabel *weightLB;

@property (nonatomic ,strong) UILabel *startTimeLB;

@property (nonatomic ,strong) UILabel *finalTimeLB;

@property (nonatomic ,strong) UILabel *noteLB;

@property (nonatomic ,strong) UILabel *nameLB;

@property (nonatomic ,strong) UIImageView *goodsOwnerIcon;

@property (nonatomic, strong) UILabel *typeLabel;

@property (nonatomic ,strong) UIButton *connectBtn;

@property (nonatomic, strong) UILabel *vipLabel;

@property (nonatomic, strong) SLShipOwnerVipLevelView *levelView;


@end

@implementation SLProductDetailViewController

- (instancetype)init{
  if (self = [super init]) {
    _isNextAvailable = YES;
  }
  return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
  [self setUpUI];
  [self addConstrains];
  [self addObservers];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:NO animated:YES];
  self.title = @"货物详情";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addObservers{
  @weakify(self)
  [[RACObserve(self.goodsVM, detailModel) filter:^BOOL(id value) {
    return value != nil;
  }] subscribeNext:^(SLGoodsDetailModel *model) {
    @strongify(self)
    [self bindDataWithModel:model];
  }];
  
 
}

- (void)bindDataWithModel:(SLGoodsDetailModel *)model{
  self.noLB.text = [NSString stringWithFormat:@"NO:%@",model.hwnum];
  self.productNameLB.text = model.hwmc;
  self.packageTypeLB.text = model.bzxs;
  self.weightLB.text = [NSString stringWithFormat:@"%@吨",model.hwzw];
  self.startLB.text = model.qsgk;
  self.finalLB.text = model.ddgk;
  self.startTimeLB.text = model.fhrq;
  self.finalTimeLB.text = model.jzrq;
  self.noteLB.text = model.xxbz;
  self.nameLB.text = model.name;
  [self.goodsOwnerIcon sd_setImageWithURL:[NSURL URLWithString:model.headPhoto] placeholderImage:[UIImage imageNamed:@"head"]];
   self.typeLabel.text = model.type == 0 ? @"船东" :@"货主";
  
  if (model.role < 1) {
    [self.vipLabel removeFromSuperview];
    [self.levelView removeFromSuperview];
  }else{
    [self.levelView configWithLevel:[NSString stringWithFormat:@"%ld",model.role]];
  }
  
}

- (void)connectGoodsOwner{
  [self.goodsVM connectGoodsOwner];
}

// 获得更多信息
- (void)getMoreInfo{
  
  if (self.isNextAvailable) {
    SLHisGoodsViewController *goodsVC = [SLHisGoodsViewController new];
    [goodsVC changeHid:self.goodsVM.detailModel.hzid];
    [self.navigationController pushViewController:goodsVC animated:YES];
  }
  
  
}

// 起点
- (void)lookStart{
  SLMapViewController *mapVC = [SLMapViewController new];
  [mapVC locationWithLon:[self.goodsVM.detailModel.qsx floatValue] lat:[self.goodsVM.detailModel.qsy floatValue]];
  [self.navigationController pushViewController:mapVC animated:YES];
}

// 终点
- (void)lookFinal{
  SLMapViewController *mapVC = [SLMapViewController new];
  [mapVC locationWithLon:[self.goodsVM.detailModel.ddx floatValue] lat:[self.goodsVM.detailModel.ddy floatValue]];
  [self.navigationController pushViewController:mapVC animated:YES];
}


#pragma mark - public methods

- (void)changeGid:(NSString *)newGid{
  _gid = newGid;
  self.goodsVM = [[SLNearlyProductDetailViewModel alloc] initWithGid:newGid];
}


#pragma mark - UI 约束

- (void)setUpUI{
  [self.view addSubview:self.mainScrollView];
  [self.view addSubview:self.connectBtn];
  [self.mainScrollView addSubview:self.contentView];
  [self.contentView addSubview:self.noLB];
  [self.contentView addSubview:self.productNameLB];
  [self.contentView addSubview:self.startLB];
  [self.contentView addSubview:self.finalLB];
  [self.contentView addSubview:self.startTimeLB];
  [self.contentView addSubview:self.finalTimeLB];
  [self.contentView addSubview:self.packageTypeLB];
  [self.contentView addSubview:self.weightLB];
  [self.contentView addSubview:self.noteLB];
  [self.contentView addSubview:self.nameLB];
  [self.contentView addSubview:self.typeLabel];
  [self.contentView addSubview:self.vipLabel];
  [self.contentView addSubview:self.levelView];
  [self.contentView addSubview:self.goodsOwnerIcon];
  
  
  UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"反馈" style:UIBarButtonItemStylePlain target:self action:@selector(toBackMsg)];
  self.navigationItem.rightBarButtonItem = rightItem;
  
}

- (void)toBackMsg{
  SLBackMsgViewController *backMsgVC = [[SLBackMsgViewController alloc] init];
  [backMsgVC backGoodsMsgWith:self.goodsVM.detailModel.hid];
  STPopupController *popVC = [[STPopupController alloc] initWithRootViewController:backMsgVC];
  [popVC presentInViewController:self];
}

- (void)addConstrains{
  
  // 线与字
  CGFloat lineMidPading = 20.f;
  // 小标题与内容
  CGFloat midPading = 12.f;
  
  [self.mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view);
  }];
  
  [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.mainScrollView);
    make.width.equalTo(self.mainScrollView);
  }];
  
  [self.noLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.contentView).offset(16);
    make.left.equalTo(self.contentView).offset(12);
    make.right.equalTo(self.contentView.mas_centerX).offset(10);
  }];
  
  [self.productNameLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.noLB);
    make.left.equalTo(self.contentView.mas_centerX).offset(2);
    make.right.equalTo(self.contentView).offset(-16);
  }];
  
#pragma mark - 第一条线
//  UIImageView *firLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
  UIView *firLine = [UIView xlineView];
  [self.contentView addSubview:firLine];
  [firLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.noLB.mas_bottom).offset(12);
    make.centerX.equalTo(self.contentView);
    make.width.equalTo(self.contentView).offset(-5);
    make.height.equalTo(@2);
  }];
  
  UILabel *startLabel = [self labelWithText:@"起点"];
  [self.contentView addSubview:startLabel];
  [startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.noLB);
    make.top.equalTo(firLine.mas_bottom).offset(lineMidPading);
  }];
  
  [self.startLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.noLB);
    make.top.equalTo(startLabel.mas_bottom).offset(midPading);
    make.width.lessThanOrEqualTo(@(SCREEN_WIDTH-40));
  }];
  
  UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [startButton setImage:[UIImage imageNamed:@"map2"] forState:UIControlStateNormal];
  [startButton addTarget:self action:@selector(lookStart) forControlEvents:UIControlEventTouchUpInside];
  [self.contentView addSubview:startButton];
  [startButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.startLB);
    make.right.equalTo(self.productNameLB);
    make.size.mas_equalTo(CGSizeMake(40, 23));
  }];
  
  
  #pragma mark - 第二条线
//  UIImageView *secLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
  UIView *secLine = [UIView xlineView];
  [self.contentView addSubview:secLine];
  [secLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.startLB.mas_bottom).offset(lineMidPading);
    make.left.right.height.equalTo(firLine);
  }];
  
  UILabel *finalLabel = [self labelWithText:@"终点"];
  [self.contentView addSubview:finalLabel];
  [finalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.noLB);
    make.top.equalTo(secLine.mas_bottom).offset(lineMidPading);
  }];
  
  [self.finalLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.noLB);
    make.top.equalTo(finalLabel.mas_bottom).offset(midPading);
  }];

  UIButton *finalButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [finalButton setImage:[UIImage imageNamed:@"map2"] forState:UIControlStateNormal];
  [finalButton addTarget:self action:@selector(lookFinal) forControlEvents:UIControlEventTouchUpInside];
  [self.contentView addSubview:finalButton];
  [finalButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.finalLB);
    make.right.equalTo(self.productNameLB);
    make.size.mas_equalTo(CGSizeMake(40, 23));
  }];
  

  #pragma mark - 第三条线
//  UIImageView *thirdLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
  UIView *thirdLine = [UIView xlineView];
  [self.contentView addSubview:thirdLine];
  [thirdLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.finalLB.mas_bottom).offset(lineMidPading);
    make.left.right.height.equalTo(firLine);
  }];
  
  UILabel *pageLabel = [self labelWithText:@"包装形式"];
  [self.contentView addSubview:pageLabel];
  [pageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(thirdLine.mas_bottom).offset(lineMidPading);
    make.left.equalTo(self.noLB);
  }];
  
  [self.packageTypeLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(pageLabel);
    make.top.equalTo(pageLabel.mas_bottom).offset(midPading);
  }];
  
  UILabel *wLabel = [self labelWithText:@"重量"];
  [self.contentView addSubview:wLabel];
  [wLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(thirdLine.mas_bottom).offset(lineMidPading);
    make.right.equalTo(self.productNameLB);
  }];
  
  [self.weightLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(wLabel);
    make.top.equalTo(pageLabel.mas_bottom).offset(midPading);
  }];
  
  
  #pragma mark - 第四条线
  UIView *forLine = [UIView xlineView];
  [self.contentView addSubview:forLine];
  [forLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.packageTypeLB.mas_bottom).offset(lineMidPading);
    make.left.right.height.equalTo(firLine);
  }];
  
  UILabel *startTimeLabel = [self labelWithText:@"发布日期"];
  [self.contentView addSubview:startTimeLabel];
  [startTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.noLB);
    make.top.equalTo(forLine.mas_bottom).offset(lineMidPading);
  }];
  
  [self.startTimeLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.noLB);
    make.top.equalTo(startTimeLabel.mas_bottom).offset(midPading);
  }];
  
  UILabel *finalTimeLabel = [self labelWithText:@"截止日期"];
  [self.contentView addSubview:finalTimeLabel];
  [finalTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.productNameLB);
    make.top.equalTo(forLine.mas_bottom).offset(lineMidPading);
  }];
  
  [self.finalTimeLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.productNameLB);
    make.top.equalTo(finalTimeLabel.mas_bottom).offset(midPading);
  }];

#pragma mark - 第五条线
  UIView *fifLine = [UIView xlineView];
  [self.contentView addSubview:fifLine];
  [fifLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.startTimeLB.mas_bottom).offset(lineMidPading);
    make.left.right.height.equalTo(firLine);
  }];
  
  UILabel *noteLabel = [self labelWithText:@"备注："];
  [self.contentView addSubview:noteLabel];
  [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.noLB);
    make.top.equalTo(fifLine.mas_bottom).offset(lineMidPading);
  }];
  
  [self.noteLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.noLB);
    make.right.equalTo(self.nameLB).offset(-5);
    make.top.equalTo(noteLabel.mas_bottom).offset(midPading);
  }];

  
#pragma mark - 第六条线
  
//  UIImageView *sixLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
  UIView *sixLine = [UIView xlineView];
  [self.contentView addSubview:sixLine];
  [sixLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.noteLB.mas_bottom).offset(lineMidPading);
    make.left.right.height.equalTo(firLine);
  }];
  
  [self.goodsOwnerIcon mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.noLB);
    make.top.equalTo(sixLine.mas_bottom).offset(lineMidPading);
    make.size.mas_equalTo(CGSizeMake(34, 34));
  }];
  
  [self.nameLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.goodsOwnerIcon.mas_right).offset(15);
    make.centerY.equalTo(self.goodsOwnerIcon);
  }];
  
  [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.nameLB.mas_right).offset(15);
    make.centerY.equalTo(self.goodsOwnerIcon);
  }];
  
  [self.vipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.typeLabel.mas_right);
    make.top.equalTo(self.typeLabel);
  }];
  
  [self.levelView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.vipLabel);
    make.left.equalTo(self.vipLabel.mas_right).offset(5);
    make.size.mas_equalTo(CGSizeMake(20, 20));
  }];
  
  UIButton *allButton = [UIButton buttonWithTitle:@"更多 >"];
  [allButton addTarget:self action:@selector(getMoreInfo) forControlEvents:UIControlEventTouchUpInside];
  [allButton setTitleColor:SLYellowColor forState:UIControlStateNormal];
  allButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
  [self.contentView addSubview:allButton];
  [allButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.nameLB);
    make.right.equalTo(self.contentView).offset(-37);
    make.size.mas_equalTo(CGSizeMake(60, 20));
  }];
  


#pragma mark - 第七条

  UIView *sevenLine = [UIView xlineView];
  [self.contentView addSubview:sevenLine];
  [sevenLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.goodsOwnerIcon.mas_bottom).offset(lineMidPading);
    make.left.right.height.equalTo(firLine);
  }];
  
  UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
  moreButton.backgroundColor = [UIColor clearColor];
  [moreButton addTarget:self action:@selector(getMoreInfo) forControlEvents:UIControlEventTouchUpInside];
  [self.contentView insertSubview:moreButton belowSubview:self.nameLB];
  [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(sixLine);
    make.bottom.equalTo(sevenLine);
    make.left.right.equalTo(sixLine);
  }];
  
  [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(sevenLine).offset(70);
  }];
  
  [self.connectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(@45);
    make.width.equalTo(self.view).offset(-10);
    make.centerX.equalTo(self.view);
    make.bottom.equalTo(self.view).offset(-5);
  }];
  
}





#pragma mark - getter

- (UILabel *)labelWithText:(NSString *)text{
  return [UILabel labelWithFontSize:14 textColor:SLInkColor text:text];
}

- (UILabel *)labelBoldWithText:(NSString *)text{
  
  return [UILabel labelwithBoldFontSize:18 textColor:[UIColor blackColor] text:text];
}

- (UILabel *)typeLabel{
  if (!_typeLabel) {
    _typeLabel = [UILabel labelWithFontSize:16 textColor:SLInkColor text:@"货主"];
  }
  return _typeLabel;
}

- (UIImageView *)goodsOwnerIcon{
  if (!_goodsOwnerIcon) {
    _goodsOwnerIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"head"]];
    _goodsOwnerIcon.backgroundColor = SLYellowColor;
  }
  return _goodsOwnerIcon;
}

- (UILabel *)startLB{
  if (!_startLB) {
    _startLB = [self labelBoldWithText:@"startLB"];
  }
  return _startLB;
}

- (UILabel *)finalLB{
  if (!_finalLB) {
    _finalLB = [self labelBoldWithText:@"finalLB"];
  }
  return _finalLB;
}

- (UILabel *)startTimeLB{
  if (!_startTimeLB) {
    _startTimeLB = [self labelBoldWithText:@"startTime"];
  }
  return _startTimeLB;
}

- (UILabel *)finalTimeLB{
  if (!_finalTimeLB) {
    _finalTimeLB = [UILabel labelwithBoldFontSize:20 textColor:SLRedColor text:@"finalTimeLB"];
  }
  return _finalTimeLB;
}

- (UILabel *)weightLB{
  if (!_weightLB) {
    _weightLB = [self labelBoldWithText:@"1000"];
  }
  return _weightLB;
}

- (UIButton *)connectBtn{
  if (!_connectBtn) {
    _connectBtn = [UIButton buttonWithTitle:@"电话联系"];
    _connectBtn.backgroundColor = SLYellowColor;
    [_connectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
     [_connectBtn addTarget:self action:@selector(connectGoodsOwner) forControlEvents:UIControlEventTouchUpInside];
  }
  return _connectBtn;
}

- (UILabel *)nameLB{
  if (!_nameLB) {
    _nameLB = [UILabel labelwithBoldFontSize:18 textColor:SLBlackColor text:@"SSSSS"];
  }
  return _nameLB;
}

- (UILabel *)noLB{
  if (!_noLB) {
    _noLB = [self labelBoldWithText:@"NO:"];
  }
  return _noLB;
}

-(UILabel *)productNameLB{
  if (!_productNameLB) {
    _productNameLB = [UILabel labelwithBoldFontSize:20 textColor:SLYellowColor text:@"productNameLB"];
    _productNameLB.textAlignment = NSTextAlignmentRight;
  }
  return _productNameLB;
}

- (UILabel *)noteLB{
  if (!_noteLB) {
    _noteLB = [self labelBoldWithText:@"SSSSS"];
    _noteLB.numberOfLines = 0;
  }
  return _noteLB;
}


- (UILabel *)packageTypeLB{
  if (!_packageTypeLB) {
    _packageTypeLB = [self labelBoldWithText:@"SSSSS"];
  }
  return _packageTypeLB;
}

- (UIView *)contentView{
  if (!_contentView) {
    _contentView = [[UIView alloc] init];
    _contentView.backgroundColor = [UIColor whiteColor];
  }
  return _contentView;
}

- (UIScrollView *)mainScrollView{
  if (!_mainScrollView) {
    _mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.backgroundColor = [UIColor defeutBackgroudColor];
  }
  return _mainScrollView;
}

-(UILabel *)vipLabel{
  if (!_vipLabel) {
    _vipLabel = [UILabel labelwithBoldFontSize:18 textColor:[UIColor redColor] text:@"VIP"];
  }
  return _vipLabel;
}

- (SLShipOwnerVipLevelView *)levelView{
  if (!_levelView) {
    _levelView = [SLShipOwnerVipLevelView new];
  }
  return _levelView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
